//
//  HyTableViewCell.m
//  BaiSiBuDeJie
//
//  Created by uproject on 16/8/29.
//  Copyright © 2016年 tsaievan. All rights reserved.
//

#import "HyTableViewCell.h"
#import "BSBaseModel.h"

@interface HyTableViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *headImg;
@property (weak, nonatomic) IBOutlet UIImageView *mainView;
@property (weak, nonatomic) IBOutlet UILabel *playCount;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *lbls;

@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UILabel *issueTime;
@property (weak, nonatomic) IBOutlet UIImageView *isVip;
@property (weak, nonatomic) IBOutlet UIImageView *yellowDiamond;
@property (weak, nonatomic) IBOutlet UILabel *text;
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic) IBOutlet UIButton *upBtn;
@property (weak, nonatomic) IBOutlet UIButton *downBtn;
@property (weak, nonatomic) IBOutlet UIButton *shareBtn;
@property (weak, nonatomic) IBOutlet UIButton *commentBtn;
@property (weak, nonatomic) IBOutlet UIImageView *gifImgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *gifHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *gifWidth;
@property (weak, nonatomic) IBOutlet UIButton *playBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgHeight;
@property (weak, nonatomic) IBOutlet UIImageView *tagIcon;

@end
@implementation HyTableViewCell

- (void)setModel:(ListModel *)model{
    _model = model;
    //头像
    NSArray *urlArr = model.u.header;
    [self.headImg sd_setImageWithURL:urlArr[0]];
    self.headImg.layer.cornerRadius = self.headImg.bounds.size.width * 0.5;
    [self.headImg clipsToBounds];
    //vip
    if (model.u.is_v) {
        self.isVip.image = [UIImage imageNamed:@"Profile_AddV_authen"];
        self.userName.textColor = [UIColor redColor];
    }else{self.isVip.image = nil;}
    
    //昵称
    self.userName.text = model.u.name;
    
    //发表时间
    self.issueTime.text = model.passtime;
    
    //标题
    self.text.text = model.text;
    //点赞数
    [self.upBtn setTitle:model.up forState:UIControlStateNormal];
    self.upBtn.selected = self.model.upBtnStatus;
    if (model.upBtnStatus) {
        [self.upBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }else{ [self.upBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];}
    //不赞
    [self.downBtn setTitle:[NSString stringWithFormat:@"%zd",model.down] forState:UIControlStateNormal];
    self.downBtn.selected = self.model.downBtnStatus;
    if (model.downBtnStatus) {
        [self.downBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }else{ [self.downBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];}
    //转载
    [self.shareBtn setTitle:[NSString stringWithFormat:@"%zd",model.forward] forState:UIControlStateNormal];
    //评论数
    [self.commentBtn setTitle:[NSString stringWithFormat:@"%@",model.comment] forState:UIControlStateNormal];
    //如果是视频
    if ([model.type isEqualToString:@"video"]) {
        NSArray *imgArr = model.video.thumbnail;
        [self.mainView sd_setImageWithURL:imgArr[0]  placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
        //播放量
        self.playCount.text = [NSString stringWithFormat:@"%zd播放",model.video.playcount];
        //视频长度
        self.time.text = [NSString stringWithFormat:@"%02zd:%02zd",model.video.duration / 60,model.video.duration % 60];
        //宽度比
        NSInteger width = model.video.width;
        NSInteger height = model.video.height;
        
        self.imgHeight.constant = (kScreenW - 20) * height / width;
        
        self.gifImgView.image = nil;
        self.gifImgView.alpha = 0;
        self.playBtn.alpha = 1;
        self.playBtn.alpha = 1;
        self.playCount.alpha = 1;
        self.time.alpha = 1;
    }
    //如果是gif
    if ([model.type isEqualToString:@"gif"]) {
        
        self.gifImgView.image = [UIImage imageNamed:@"common-gif"];
        
        NSArray *gifArr = model.gif.images;
        
        [self.mainView sd_setImageWithURL:gifArr[0] placeholderImage:[UIImage imageNamed:@"placeholderImage"] ];
        
        
        //宽高比
        NSInteger width = model.gif.width;
        NSInteger height = model.gif.height;
        
        self.imgHeight.constant = (kScreenW - 20) * height / width;
        
        self.gifImgView.alpha = 1;
        self.playBtn.alpha = 0;
        self.playCount.alpha = 0;
        self.time.alpha = 0;
    }
    
    //image图片
    if ([model.type isEqualToString:@"image"]) {
        NSLog(@"111");
        self.gifImgView.image = nil;
        self.gifImgView.alpha = 0;
        
        NSArray *imageArr = model.image.big;
        
        [self.mainView sd_setImageWithURL:imageArr[0] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
        
        //宽高比
        NSInteger width = model.image.width;
        NSInteger height = model.image.height;
        
        self.imgHeight.constant = (kScreenW - 20) * height / width;
        
//        if ((kScreenW - 20) * height / width > 300) {
//            self.imgHeight.constant = 300;
//        }
        self.playBtn.alpha = 0;
        self.playCount.alpha = 0;
        self.time.alpha = 0;
  
    }
    
    //声音
    if ([model.type isEqualToString:@"audio"]) {
        NSArray *audioArr = model.audio.thumbnail;
        [self.mainView sd_setImageWithURL:audioArr[0] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
        
        NSInteger width = model.audio.width;
        NSInteger height = model.audio.height;
        self.imgHeight.constant = (kScreenW - 20) * height / width;
        self.gifImgView.image = nil;
        self.gifImgView.alpha = 0;
        self.playBtn.alpha = 0;
        self.playCount.alpha = 0;
        self.time.alpha = 0;
//        self.imageView.size = CGSizeMake(width, height);

    }

    if ([self.model.type isEqualToString:@"text"])
    {
        //文本的时候把图片撤掉
        self.imgHeight.constant = 0;
        self.gifImgView.alpha = 0;
        self.playBtn.alpha = 0;
        self.playCount.alpha = 0;
        self.time.alpha = 0;
    }
    //创建标签
    for (UILabel *lbl in self.lbls) {
        lbl.text = nil;
    }
    for (NSInteger i = 0; i < model.tags.count; i ++) {
        UILabel *lab = self.lbls[i];
        
        lab.text = model.tags[i].name;
    }
    
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

@end
